// pages/bindCard/addcard/index.js
let app = getApp();
const HttpAjax = getApp().getHttpData;
import Toast from '../../../components/dist/toast/toast';
import {Base64Encrypt,Base64Decrypt,Decrypt,EncryptAES,SHA256,RSA_encrypt,RSA} from '../../../utils/crypto_RSA_AES.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomerName:'',
    IDCode: '',
    pwd:'',
    radio:'1'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.cardid);
    var idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    var TEL_REGEXP = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
    var cardNo = app.globalData.cardid.trim()

    if (idcardReg.test(cardNo)) {
      this.setData({
        IDCode: cardNo,
        radio:'1'
      })
    } else if (TEL_REGEXP.test(cardNo)) {
      this.setData({
        IDCode: cardNo,
        radio: '2'
      })
    }else{
      this.setData({
        IDCode: cardNo,
        radio: '3'
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange(e){
    this.setData({
      radio:e.detail
    })
  },
  blur1(e) {
    // console.log(e.detail.value);
    this.setData({
      CustomerName: e.detail.value
    })
  },
  blur2(e) {
    // console.log(e.detail.value)
    this.setData({
      IDCode: e.detail.value
    })
  },
  blur3(e){
    // console.log(e.detail.value)
    this.setData({
      pwd: e.detail.value
    })
  },
  emit(){
    Toast.loading({
      mask: true,
      duration:0,
      message: '添加中...'
    });
    setTimeout(()=>{
      let parma = {};
      parma.CustomerName = this.data.CustomerName
      parma.IDType = this.data.radio;
      parma.IDCode = this.data.IDCode;
      parma.pwd = this.data.pwd;
      // console.log(parma)
      // return
      const params = [
        this.data.CustomerName,
        this.data.IDCode,
        this.data.pwd
      ];
      let Loginciphertext = RSA_encrypt().encrypt(params.join(','));
      HttpAjax(app.globalData.url + '/Door/AddCustomer', {Loginciphertext:Loginciphertext,IDType:this.data.radio}, 'post', app.globalData.headerCookie)
        .then(ress => {
          Toast.clear();
          let res = ress.data;
          if (res.result == 1) {
            Toast(`添加成功`);
            wx.navigateBack();
          }
          if (res.result == -2) {
            Toast(`账套不存在`);
          }
          if (res.result == -3) {
            Toast(`人员不存在`);
          }
          if (res.result == -4) {
            Toast(`人员密码错误`);
          }
          if (res.result == 2) {
            Toast(`此账套已存在,请添加其他账套`);
          }
          if (res.result == -5) {
            if (this.data.radio==1){
              Toast(`绑定的卡号被多人使用`);
            }
            if (this.data.radio == 2) {
              Toast(`绑定的手机号被多人使用`);
            }
            if (this.data.radio == 3) {
              Toast(`绑定的身份证号被多人使用`);
            }
            
          }
        })
        .catch(err=>{
          Toast.clear()
        })
    },500)
    
  }
})